#include "LuaLink_Trace.h"

LuaLink_Trace::LuaLink_Trace(MyGUI::Gui* gui) {
	this->gui = gui;
}

LuaLink_Trace::~LuaLink_Trace(void) {
	if (GAME_LOG != NULL) {
		Ogre::LogManager::getSingletonPtr()->destroyLog(GAME_LOG);
		GAME_LOG = NULL;
	}
}

MyGUI::Gui* LuaLink_Trace::gui = NULL;
Ogre::Log* LuaLink_Trace::GAME_LOG = NULL;
MyGUI::Window* LuaLink_Trace::TRACE_WINDOW = NULL;
MyGUI::Edit* LuaLink_Trace::TRACE_EDIT = NULL;

void LuaLink_Trace::trace(const char* msg) {
	if (GAME_LOG == NULL) {
		GAME_LOG = Ogre::LogManager::getSingletonPtr()->createLog("game.log", false, false, false);
	}
	GAME_LOG->logMessage(msg);

	if (TRACE_EDIT != NULL) {
		if (TRACE_EDIT->getTextLength() > 1200) {
			std::string valor = TRACE_EDIT->getText(0, TRACE_EDIT->getTextLength());
			TRACE_EDIT->setCaption(valor.substr(100));
		}
		std::string str = msg;
		TRACE_EDIT->addText(str + "\n");
	}
}

void LuaLink_Trace::debug(bool mode) {
	if (mode) {
		if (TRACE_WINDOW == NULL) {
			MyGUI::LayoutManager::getInstancePtr()->load("Trace.layout");
			TRACE_WINDOW = gui->findWidget<MyGUI::Window>("Trace_Window");
			TRACE_EDIT = gui->findWidget<MyGUI::Edit>("Trace_Edit");
			TRACE_WINDOW->setAutoAlpha(false);
		}
		if (TRACE_WINDOW != NULL) {
			TRACE_WINDOW->showSmooth(true);
		}
	} else {
		if (TRACE_WINDOW != NULL) {
			TRACE_WINDOW->hideSmooth();
		}
	}
}
